package server.layer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Awake
 * Date: 24.03.12
 * Time: 17:38
 * To change this template use File | Settings | File Templates.
 */
public class ServiceHandler {
    private ArrayList<Person> persons = new ArrayList<Person>();
    JsonUtil util = new JsonUtil();
    private static String DEFAULT_IMAGE = "E:/for_db/Pedobearseal.jpg";


    public void receive(String str, Socket socket) {
        try {
            JSONObject fullStr = new JSONObject(str);
            ServiceProtocol command = ServiceProtocol.getCommand(fullStr.getString("command"));
            switch (command) {
                case GET_DESCRIPTION:
                    Long id = fullStr.getLong("argument");
                    sendDescription(socket, id);
                    sendImage(socket,id);
                    break;
                case CONNECT:
                    sendList(socket);
                    break;
            }

        } catch (JSONException e1) {
            e1.printStackTrace();
        }


    }

    public void sendList(Socket socket) {
        this.connectDB();
        JSONObject fullCommand = new JSONObject();
        JSONArray array = util.arrayToJson(persons);
        try {
            fullCommand.put("array", array);
            fullCommand.put("command", ServiceProtocol.UPDATE_LIST.getAbbr());
            ServerThread.server.sendMessageTo(socket, fullCommand.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void sendDescription(Socket socket, Long id) {
        PersonDao p = new PersonDao();
        JSONObject fullCommand = new JSONObject();
        try {
            String description = p.getDescriptionByID(id);
            fullCommand.put("command", ServiceProtocol.SEND_DESCRIPTION.getAbbr());
            fullCommand.put("description", description);
            fullCommand.put("id", id);
            ServerThread.server.sendMessageTo(socket, fullCommand.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void sendImage(Socket socket, Long id) {
        PersonDao p = new PersonDao();
        BufferedImage img;
        try {
            String imgPath = p.getImagePath(id);
            imgPath = (imgPath != null && !imgPath.isEmpty()) ? imgPath : DEFAULT_IMAGE;
            img = ImageIO.read(new File(imgPath));
            ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
            ImageIO.write(img, "JPG", imageBuffer);
            imageBuffer.flush();
            imageBuffer.close();
            byte[] data = imageBuffer.toByteArray();
           // getByID(id).setImgByte(data);                   БЛЯ ЧТО ЗА ХУИТА ПОТОМ ВЫЯСНИ !!!

            JSONObject fullCommand = new JSONObject();
            fullCommand.put("command", ServiceProtocol.SEND_IMAGE.getAbbr());
            JSONArray jImg = new JSONArray(data);

            System.out.println(jImg);

            fullCommand.put("img" , jImg);
            ServerThread.server.sendMessageTo(socket, fullCommand.toString());


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public Person getByID(Long id) {
        for (Person p : persons) {
            if (id.equals(p.getPerson_id())) {
                return p;
            }
        }
        return null;
    }


    public void connectDB() {
        PersonDao p = new PersonDao();
        persons.clear();
        try {
            p.getAllPersons(persons);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
